package com.jacob.androidframe.base;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EFragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * 本类是所有Fragment的父类。
 * 用来封装和业务无关的复杂代码。
 * 
 * @author 王超
 *
 */
@EFragment
public abstract class FragmentBase extends Fragment
{
	/**
	 * 当Fragment和Activity关联后调用。
	 * 
	 * @param pActivity 主Activity
	 */
	@Override
	public void onAttach(Activity pActivity)
	{
		super.onAttach(pActivity);
	}

	/**
	 * 创建Fragment时，系统调用本方法。
	 * 重写本方法时，应该初始化Fragment的重要组件。
	 * 在Fragment被暂停、停止，然后恢复时，你想要保持的都算是重要组件。
	 */
	@Override
	public void onCreate(Bundle pSavedInstanceState)
	{
		super.onCreate(pSavedInstanceState);
		initOnCreate(pSavedInstanceState);
	}

	/**
	 * 当Fragment首次要画用户界面时，系统调用本方法。
	 * 本方法必须返回Fragment的Layout中的根部View。
	 * 若Fragment不提供用户界面，可以返回null。
	 */
	@Override
	public View onCreateView(LayoutInflater pInflater, ViewGroup pContainer, Bundle pSavedInstanceState)
	{
		return super.onCreateView(pInflater, pContainer, pSavedInstanceState);
	}

	/**
	 * 当主Activity的onCreate()方法返回时，系统调用本方法。
	 * 本方法中，一般应该
     * 如：初始化Loader。
	 * 
	 */
	@Override
	public void onActivityCreated(Bundle pSavedInstanceState)
	{
		super.onActivityCreated(pSavedInstanceState);
	}

    /**
     * Called when the Fragment is visible to the user.  This is generally
     * tied to {@link Activity#onStart() Activity.onStart} of the containing
     * Activity's lifecycle.
     */
	@Override
	public void onStart()
	{
		super.onStart();
	}
	
    /**
     * Called when the fragment is visible to the user and actively running.
     * This is generally
     * tied to {@link Activity#onResume() Activity.onResume} of the containing
     * Activity's lifecycle.
     */
	@Override
	public void onResume()
	{
		super.onResume();
	}
	
	/**
	 * 用户离开Fragment时（这并不总是说Fragment被销毁），系统第一个调用本方法。
	 * 本方法中，应该提交当前用户会话所做的任何永久性的更改。（因为用户可能不会回来了）
	 */
	@Override
	public void onPause()
	{
		super.onPause();
	}

	@Override
	public void onStop()
	{
		super.onStop();
	}

	/**
	 * 当和Fragment关联的View层级结构被删除时，系统调用本方法。
	 */
	@Override
	public void onDestroyView()
	{
		super.onDestroyView();
	}

	/**
	 * Called to ask the fragment to save its current dynamic state, so it
	 * can later be reconstructed in a new instance of its process is
	 * restarted.  If a new instance of the fragment later needs to be
	 * created, the data you place in the Bundle here will be available
	 * in the Bundle given to {@link #onCreate(Bundle)},
	 * {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}, and
	 * {@link #onActivityCreated(Bundle)}.
	 *
	 * <p>This corresponds to {@link Activity#onSaveInstanceState(Bundle)
	 * Activity.onSaveInstanceState(Bundle)} and most of the discussion there
	 * applies here as well.  Note however: <em>this method may be called
	 * at any time before {@link #onDestroy()}</em>.  There are many situations
	 * where a fragment may be mostly torn down (such as when placed on the
	 * back stack with no UI showing), but its state will not be saved until
	 * its owning activity actually needs to save its state.
	 *
	 * @param outState Bundle in which to place your saved state.
	 */
	@Override
	public void onSaveInstanceState(Bundle pOutState)
	{
		super.onSaveInstanceState(pOutState);
	}

	@Override
	public void onDestroy()
	{
		super.onDestroy();
	}

	/**
	 * 当Fragment和Actiity取消关联时，系统调用本方法。
	 */
	@Override
	public void onDetach()
	{
		super.onDetach();
	}
	
	@AfterInject
	protected abstract void afterInject();
	
	protected abstract void initOnCreate(Bundle pSavedInstanceState);
	
	@AfterViews
	protected abstract void afterViews();
}
